﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using DTO;
using BUS;
using Telerik.Web;

namespace TCV_WEB.Admin.Page
{
    public partial class DetailNews : System.Web.UI.Page
    {

        private string _sErr = "";
        public string strUploadFolder;
        public string Username;
        public static int ContentLength;
        private string name_image = "";
        private bool iflag = false;
        private long inewid;


        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                // pro = new MenuProduct(strLanguageID);
                Reset();


                if (Session["newsid"] != null)
                {
                    LoadCombo1();
                    LoadData(Convert.ToInt32(Session["newsid"]));
                    iflag = true;
                    inewid = Convert.ToInt64(Session["newsid"]);
                }
                else
                {
                    this.LoadCombo();
                }
            }

        }

        protected void btReset_Click(object sender, EventArgs e)
        {
            Reset();
        }

        public void LoadCombo()
        {

            NewsCategoriesControl newscat_control = new NewsCategoriesControl();
            DataTable dt = newscat_control.GetAll(ref _sErr);
            ddownlistnew.DataSource = dt;
            ddownlistnew.DataTextField = "NameVN";
            ddownlistnew.DataValueField = "ID";
            ddownlistnew.DataBind();

            int catid = Convert.ToInt32(Session["newcatid"]);
            NewsCategoriesInfo newscat_info = new NewsCategoriesInfo();
            newscat_info = newscat_control.Get(catid, ref _sErr);
            ddownlistnew.SelectedValue = newscat_info.ID.ToString();
        }

        public void LoadCombo1()
        {

            NewsCategoriesControl newscat_control = new NewsCategoriesControl();
            DataTable dt = newscat_control.GetAll(ref _sErr);
            ddownlistnew.DataSource = dt;
            ddownlistnew.DataTextField = "NameVN";
            ddownlistnew.DataValueField = "ID";
            ddownlistnew.DataBind();

            NewsControl news_control = new NewsControl();
            NewsInfo new_info = new NewsInfo();
            new_info = news_control.Get(Convert.ToInt64(Session["newsid"]), ref _sErr);
            ddownlistnew.SelectedValue = new_info.IDNewsCategory.ToString();

        }

        private void LoadData(int strProductID)
        {
            //    ne = new Product(strLanguageID);
            NewsInfo news_info = new NewsInfo();
            NewsControl news_control = new NewsControl();


            strUploadFolder = ConfigurationManager.AppSettings["UploadFolder"].ToString()+"Images/";
            //  ne.ProductID = strProductID;
            try
            {
                news_info = news_control.Get(Convert.ToInt64(strProductID), ref _sErr);


                if (radiolistlanguage.SelectedValue == "En")
                {
                    radshorteditor.Content = news_info.ShortDecribes;
                    radlongeditor.Content = news_info.LongDecribes;
                    txtTitle.Text = news_info.Title;
                }
                else
                {
                    radshorteditor.Content = news_info.ShortDecribesVN;
                    radlongeditor.Content = news_info.LongDecribesVN;
                    txtTitle.Text = news_info.TitleVN;
                }

                // FCKeditor_short.Value = news_info.ShortDecribesVN;
                // FCKeditor_long.Value = news_info.LongDecribesVN;
                if (news_info.Image == "")
                    img.ImageUrl = strUploadFolder + "noimages.jpg";
                else
                    img.ImageUrl = strUploadFolder + news_info.Image;

                rblIsViewed.SelectedValue = news_info.IDStatus.ToString();
                txtSource.Text = news_info.Source;
            }
            catch (System.Exception e)
            {
                lblErr.Text = e.Message;
            }

        }

        private void Reset()
        {
            txtTitle.Text = "";
            txtSource.Text = "";
            // FCKeditor_short.Value = "";
            // FCKeditor_long.Value = "";
            img.ImageUrl = "~/Uploads/Image/noimages.jpg";
            cbpic.Checked = false;
            lblErr.Text = "";
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            NewsInfo news_info = new NewsInfo();
            NewsControl news_control = new NewsControl();

            if (Session["newsid"] != null)
            {
                news_info = news_control.Get(Convert.ToInt64(Session["newsid"].ToString()), ref _sErr);
            }

            lblErr.Text = "";
            news_info.IDNewsCategory = Convert.ToInt32(ddownlistnew.SelectedValue);
            news_info.IDStatus = Convert.ToInt32(rblIsViewed.Text);
            if (radiolistlanguage.SelectedValue == "En")
            {

                news_info.ShortDecribes = radshorteditor.Content;
                news_info.LongDecribes = radlongeditor.Content;
                news_info.Title = txtTitle.Text;
            }
            else
            {
                news_info.ShortDecribesVN = radshorteditor.Content;
                news_info.LongDecribesVN = radlongeditor.Content;
                news_info.TitleVN = txtTitle.Text;
            }

            news_info.Source = txtSource.Text;

            if (FileUpload_image.FileName != "" && cbpic.Checked == false)
                news_info.Image = upload_file();
            else
            {
                String[] s = img.ImageUrl.Split(new char[] { '/' });
                news_info.Image = s[s.Length - 1];
            }
            news_info.CountPreview = 1;
            news_info.CreateDay = DateTime.Now;

            if (lblErr.Text == "")
            {

                try
                {
                    if (Session["newsid"] != null)
                    {
                        //news_info.ID = Convert.ToInt64(Session["newsid"]);
                        //NewsInfo temp = new NewsInfo();
                        //temp = news_control.Get_News((int)news_info.ID, ref _sErr);
                        //news_info.CountPreview = temp.CountPreview;

                        news_control.Update(news_info);
                        lblErr.Text = "Update successful...";
                        LoadData((int)news_info.ID);
                    }
                    else
                    {

                        news_control.Add(news_info, ref _sErr);
                        lblErr.Text = "Add successful...";
                        //      Reset();
                    }


                }
                catch (System.Exception ex)
                {
                    lblErr.Text = ex.Message.ToString();
                }
            }
        }

        protected string upload_file()
        {
            FileUpload fupload = FileUpload_image.FindControl("FileUpload_image") as FileUpload;
            HttpPostedFile postFile = fupload.PostedFile;
            strUploadFolder = ConfigurationManager.AppSettings["UploadFolder"].ToString() + "Images/";
            ContentLength = Convert.ToInt32(ConfigurationManager.AppSettings["ContentLength"].ToString());
            if (fupload.HasFile == true)
            {
                if (postFile.ContentLength < ContentLength)
                {
                    try
                    {
                        string strImagePath = Server.MapPath("~/" + strUploadFolder + fupload.FileName);
                        fupload.SaveAs(strImagePath);
                        img.ImageUrl = strUploadFolder + fupload.FileName;
                        name_image = fupload.FileName;
                    }
                    catch (Exception ex)
                    {
                        lblErr.Text = ex.Message;
                    }
                }
                else
                {
                    lblErr.Text = "Vui lòng chọn file ảnh có kích thước nhỏ hơn 200kb";
                }

            }
            else
            {

                lblErr.Text = " file đã tồn tại"; //"Vui lòng chọn file ảnh có kích thước nhỏ hơn 200kb";
            }
            return name_image;
        }

        protected void btCancel_Click(object sender, EventArgs e)
        {
            Session["newsid"] = null;
            Response.Redirect("~/Admin/Page/NewsCategories.aspx");

        }

        protected void radiolistlanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData(Convert.ToInt32(Session["newsid"]));
        }
    }
}
